package hy.mvpdemo.view.impl;


import android.content.Context;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.widget.RelativeLayout;
import android.widget.Toast;

import java.util.List;

import hy.mvpdemo.R;
import hy.mvpdemo.adpter.NewsAdapter;
import hy.mvpdemo.model.bean.NewsBean;
import hy.mvpdemo.presenter.NewsDataPresenterInterface;
import hy.mvpdemo.presenter.impl.NewsDataPresenterImpl;
import hy.mvpdemo.view.NewsView;

public class MainActivity extends AppCompatActivity implements NewsView<NewsBean.TngouBean>, SwipeRefreshLayout.OnRefreshListener {

    private android.support.v7.widget.RecyclerView rvnewlist;
    private Context context;
    private SwipeRefreshLayout sp_refresh;
    private NewsDataPresenterInterface newsPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        context = this;
        this.rvnewlist = (RecyclerView) findViewById(R.id.rv_newlist);
        this.sp_refresh = (SwipeRefreshLayout) findViewById(R.id.sp_refresh);

        newsPresenter = new NewsDataPresenterImpl(this);
        newsPresenter.loadDataofModel();
        sp_refresh.setOnRefreshListener(this);
    }


    @Override
    public void LoadListDataforView(List<NewsBean.TngouBean> newsdata) {
        rvnewlist.setAdapter(new NewsAdapter(context, newsdata));
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        StaggeredGridLayoutManager staggeredGridLayoutManager
                = new StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.VERTICAL);

        rvnewlist.setLayoutManager(staggeredGridLayoutManager);

        if(sp_refresh.isRefreshing()){
            sp_refresh.setRefreshing(false);
        }
    }

    @Override
    public void loadError(String error) {
        Toast.makeText(context, error, Toast.LENGTH_SHORT).show();
    }


    @Override
    public void onRefresh() {
        newsPresenter.loadDataofModel();
    }
}
